package com.csun.nusing.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.FollowUpDeviceBloodPressJsonBean;
import com.csun.nusing.bean.jsonbean.FollowUpDeviceBloodPressJsonBean.FollowUpDeviceBloodPressDataBean;
import com.csun.nusing.bean.jsonbean.FollowUpDeviceBloodPressJsonBean.FollowUpDeviceBloodPressDataBean.FollowUpDeviceBloodPressListBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.presenter.inter.IBase;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.ToastUtil;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * 血压测试的历史界面 ClassName: BloodPressTestActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-25
 */
public class BloodPressHistoryActivity extends BaseActivity<CommonPresenter>
		implements IBase {

	private ListView hislv;
	private String olderId;
	private String authorization;
	protected FollowUpDeviceBloodPressJsonBean followhistorybpbean;
	private MyBaseAdapter<FollowUpDeviceBloodPressListBean> adapter;
	protected TextView timetv;
	protected TextView datatv;
	protected TextView statetv;
	protected SimpleDateFormat sdf;
	private RelativeLayout navrl;
	private boolean isFamily;

	@Override
	public int getContentViewId() {
		return R.layout.activity_blood_press_history;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView();
	}

	/**
	 * 找到控件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-26
	 */
	private void findView() {
		hislv = findViewById(R.id.bloodpress_history_date_lv);
	}

	@Override
	public void initData() {
		super.initData();
		olderId = getIntent().getStringExtra("olderId");
		authorization = (String) SPUtils.get(this, "authorization", "");
		navrl = findViewById(R.id.nav_rl);
		isFamily = getIntent().getBooleanExtra("isFamily", false);
		if (isFamily) {
			navrl.setBackgroundResource(R.drawable.main_whiteyello_color_shape);	
		}
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("id", olderId);
		param.put("pageNo", 1 + "");
		param.put("pageSize", "1000");
		OkHttpTask.newInstance(this).addHeader("authorization", authorization)
				.post(param, OkType.form).enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						Gson gson = new Gson();
						if (result != null) {
							Log.e("fzq", "---------------"+result);
							followhistorybpbean = gson.fromJson(result,
									FollowUpDeviceBloodPressJsonBean.class);
							if (followhistorybpbean != null) {
								if (followhistorybpbean.getData() != null) {
									controlSearchDate(followhistorybpbean
											.getData());
								} else {
									ToastUtil.showMessage(
											getApplicationContext(),
											followhistorybpbean.getMessage());
								}
							}
						}
					}
				}).start(UrlBean.HISTORYBPRESSUREURL);
	}

	protected void controlSearchDate(FollowUpDeviceBloodPressDataBean data) {
		if (data != null && data.getList() != null) {
			adapter = new MyBaseAdapter<FollowUpDeviceBloodPressListBean>(this,
					data.getList(), R.layout.item_follow_history) {
				@Override
				public void convert(ViewHolder helper,int position,
						FollowUpDeviceBloodPressListBean item) {
					timetv = helper
							.getView(R.id.item_follow_history_day_tv);
					datatv = helper
							.getView(R.id.item_follow_history_data_tv);
					statetv = helper
							.getView(R.id.item_follow_history_state_tv);
					sdf = new SimpleDateFormat("MM月dd日 HH时");
					Date date = new Date(item.getModifyTime());
					timetv.setText(""+sdf.format(date));
					datatv.setText(""+item.getDbp() + "/" + item.getSbp());
					statetv.setText("" + item.getStatus());
				}
			};
			hislv.setAdapter(adapter);
		}
	}

}